package com.db.compare.controller;

import com.alibaba.fastjson.JSON;
import com.db.compare.core.BaseResponse;
import com.db.compare.model.DbCompareReport;
import com.db.compare.mapper.DbCompareReportMapper;
import com.db.compare.service.DbCompareReportService;
import com.db.compare.vo.DbCompareReportVo;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.db.compare.core.BaseController;

import java.util.Map;
/**
* 消息通知
* */
@Api("消息通知")
@Controller
@RequestMapping("/db/compare/report")
public class DbCompareReportController extends BaseController{

    @Autowired
    private DbCompareReportMapper dbcomparereportMapper;
    @Autowired
    private DbCompareReportService dbcomparereportService;

    @RequestMapping(value = {"/list",""})
    public String dbcomparereportList(){
        return "/html/DbCompareReport";
    }

    @RequestMapping("/pages")
    @ResponseBody
    public PageInfo<DbCompareReport> pages(Model model,@RequestParam Map dbcomparereportMap){
        DbCompareReportVo dbcomparereportVo = JSON.parseObject(JSON.toJSONString(dbcomparereportMap), DbCompareReportVo.class);
        return dbcomparereportService.pageInfo(dbcomparereportVo);
    }

    @ApiOperation("删除")
    @RequestMapping("/del")
    @ResponseBody
    public BaseResponse del(@RequestParam String ids){
        System.out.println(ids);
        String[] arrays = ids.split(",");
        dbcomparereportMapper.deleteByBatch(arrays);
        return new BaseResponse();
    }

    @RequestMapping("/add")
    @ResponseBody
    public BaseResponse add(@RequestBody DbCompareReport dbcomparereport){
        if(dbcomparereport.getId()==null) {
            dbcomparereport.setCreateTime(System.currentTimeMillis());
            dbcomparereportMapper.insert(dbcomparereport);
        }else{
            dbcomparereport.setCreateTime(System.currentTimeMillis());
            dbcomparereportMapper.updateByPrimaryKey(dbcomparereport);
        }
        return new BaseResponse(200,dbcomparereport);
    }
    @RequestMapping("/get")
    @ResponseBody
    public BaseResponse get(@RequestParam String id){
        int pk=Integer.parseInt(id);
        DbCompareReport dbcomparereport = dbcomparereportMapper.selectByPrimaryKey(pk);
        dbcomparereport.setRead(1);
        dbcomparereportMapper.updateByPrimaryKey(dbcomparereport);
        return new BaseResponse(200,dbcomparereport);
    }

}
